package com.cn.xiaonuo.slbfinances.stockcontrol.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.stockcontrol.entity.SlShopStockControl;
import com.cn.xiaonuo.slbfinances.stockcontrol.entity.SlStoreInventoryTotal;
import com.cn.xiaonuo.slbfinances.stockcontrol.enums.FactoryStockEnum;
import com.cn.xiaonuo.slbfinances.stockcontrol.mapper.SlShopStockControlMapper;
import com.cn.xiaonuo.slbfinances.stockcontrol.mapper.SlStoreInventoryTotalMapper;
import com.cn.xiaonuo.slbfinances.stockcontrol.param.SlShopStockControlParam;
import com.cn.xiaonuo.slbfinances.stockcontrol.param.SlStoreInventoryTotalParam;
import com.cn.xiaonuo.slbfinances.stockcontrol.service.FactoryStockControlService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author: luyangyang
 * @CreateTime: 2023-01-10  15:37
 * @Description: 供应商库存类
 */
@Service
@Slf4j
public class FactoryStockControlServiceImpl implements FactoryStockControlService {
    @Autowired
    private SlShopStockControlMapper slShopStockControlMapper;
    @Autowired
    private SlStoreInventoryTotalMapper slStoreInventoryTotalMapper;
    /**
     * 供应商库存查询
     * @param param
     * @return
     */
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage queryPage(SlStoreInventoryTotalParam param){
        BusinessMessage message = new BusinessMessage();
        Integer pageSize = param.getPageSize();
        Integer pageNo = param.getPageNo();
        if (null == pageNo || pageNo < 1) {
            pageNo=1;
        }
        if (null == pageSize || pageSize < 1) {
            pageSize=10;
        }
        LambdaQueryWrapper<SlStoreInventoryTotal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SlStoreInventoryTotal::getType, FactoryStockEnum.FACTORY_STOCK_TYPE.getCode().intValue());
        if (ObjectUtils.isNotEmpty(param.getTransactionBeginTime()) || ObjectUtils.isNotEmpty(param.getTransactionEndTime())) {
            wrapper.between(SlStoreInventoryTotal::getTransactionTime, param.getTransactionBeginTime(), param.getTransactionEndTime());
        }
        if (ObjectUtils.isNotEmpty(param.getShopName())) {
            wrapper.like(SlStoreInventoryTotal::getShopName, param.getShopName());
        }
        wrapper.orderByDesc(SlStoreInventoryTotal::getId);
        Page<SlStoreInventoryTotal> page = slStoreInventoryTotalMapper.selectPage(new Page<>(pageNo, pageSize), wrapper);
        message.setData(page);
        message.setSuccess(true);
        message.setMessage("操作成功");
        return message;
    }
    /**
     * 供应商库存查询
     * @param param
     * @return
     */
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage queryDetail(SlShopStockControlParam param){
        BusinessMessage message = new BusinessMessage();
        SlShopStockControl slShopStockControl=new SlShopStockControl();
        BeanUtil.copyProperties(param,slShopStockControl);
        Integer pageSize = param.getPageSize();
        Integer pageNo = param.getPageNo();
        if (null == pageNo || pageNo < 1) {
            pageNo=1;
        }
        if (null == pageSize || pageSize < 1) {
            pageSize=10;
        }
        LambdaQueryWrapper<SlShopStockControl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SlShopStockControl::getType, FactoryStockEnum.FACTORY_STOCK_TYPE.getCode().intValue());
        if (ObjectUtils.isNotEmpty(param.getDocumentNo())) {
            wrapper.eq(SlShopStockControl::getDocumentNo, param.getDocumentNo());
        }
        if (ObjectUtils.isNotEmpty(param.getShopId())) {
            wrapper.eq(SlShopStockControl::getShopId, param.getShopId());
        }
        if (ObjectUtils.isNotEmpty(param.getShopName())) {
            wrapper.like(SlShopStockControl::getShopName, param.getShopName());
        }
        if (ObjectUtils.isNotEmpty(param.getProductName())) {
            wrapper.like(SlShopStockControl::getProductName, param.getProductName());
        }
        if (ObjectUtils.isNotEmpty(param.getTransactionBeginTime()) || ObjectUtils.isNotEmpty(param.getTransactionEndTime())) {
            wrapper.between(SlShopStockControl::getTransactionTime, param.getTransactionBeginTime(), param.getTransactionEndTime());
        }
        if (ObjectUtils.isNotEmpty(param.getPurchaseShopId())) {
            wrapper.eq(SlShopStockControl::getPurchaseShopId, param.getPurchaseShopId());
        }
        if (ObjectUtils.isNotEmpty(param.getPurchaseShopName())) {
            wrapper.like(SlShopStockControl::getPurchaseShopName, param.getPurchaseShopName());
        }
        if (ObjectUtils.isNotEmpty(param.getStockType())) {
            wrapper.eq(SlShopStockControl::getStockType, param.getStockType());
        }
        if (ObjectUtils.isNotEmpty(param.getTradeType())&&"1".equals(param.getTradeType())) {
            List<Integer> roleIdList=new ArrayList<>();
            roleIdList.add(1);
            roleIdList.add(2);
            wrapper.in(SlShopStockControl::getStockType, roleIdList);
        }
        wrapper.orderByDesc(SlShopStockControl::getId);
        Page<SlShopStockControl> page = slShopStockControlMapper.selectPage(new Page<>(pageNo, pageSize), wrapper);
        message.setData(page);
        message.setSuccess(true);
        message.setMessage("操作成功");
        return message;
    }
}
